package com.hz.ys_machineTaskInfo.action;


import java.util.ArrayList;
import java.util.List;

import com.hz.framework.MyAction.BaseAction;
import com.hz.framework.ajax.BPComDwr;
import com.hz.framework.dao.CommonDao;
import com.hz.framework.util.CommonUtils;
import com.hz.framework.util.DateUtil;
import com.hz.framework.util.SqlParameter;
import com.hz.ys_financial.dao.FinancialDao;
import com.hz.ys_machineTaskInfo.bean.Machinetask;
import com.hz.ys_system.dbsql.SystemSql;

public class MachinetaskorderAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private Machinetask mto ;
	CommonDao cmd = new CommonDao();
	public Machinetask getMto() {
		return mto;
	}
	public void setMto(Machinetask mto) {
		this.mto = mto;
	}
	/**
	 * 插入机台排产任务
	 * 关联机台与任务单
	 * @return
	 * @throws Exception
	 */
	public String ins() throws Exception{

		String isreset=request.getParameter("isreset");//是否重新选择
		
		mto.setCreatetime(DateUtil.getDateTime());
		mto.setMtoid(CommonUtils.getCharacterAndNumber());
		mto.setOrderuser(request.getSession().getAttribute("userid")+"");
		mto.setStatus("未开工");
		if(notNull(isreset)&&!"null".equals(isreset)){
			//System.out.println(request.getParameter("mtid"));
			//System.out.println("update machinetask set machineid='"+mto.getMachineid()+"' where id="+request.getParameter("mtid"));
			cmd.executeUpdate("update machinetask set machineid='"+mto.getMachineid()+"' where id="+request.getParameter("mtid"));
		}else{
			cmd.insertinfor(mto);
		}
		return SUCCESS;
	}
	/**
	 * 查询设备列表
	 * @return
	 * @throws Exception
	 */
	public String machineList() throws Exception{
		String isreset=request.getParameter("isreset");//是否重新选择
		String taskid=request.getParameter("taskid");//任务单编号
		String sql;
		//传递taskid
		request.setAttribute("taskid",taskid);
		request.setAttribute("isreset",isreset);
		sql = SystemSql.SEL_MACHINE_INFO+" and applyflag='y'";
		doAllSearchList(request, "list", sql, "jdbc");
		return SUCCESS;
	}
	
	/**
	 * @author    修改: 韩飞
	 * @time Jan 6, 2012	
	 * @return
	 * @throws Exception 
	 * 方法功能: 机台列表 查出未开工但有工单的设备和对应工单数量
	 */
	public String machineOrderList() throws Exception{

//		String sql = "select ad.mid,ad.tasknum,ad.machinename,ad.manufacturer,ad.machinetype,ad.standard,ad.operater,ad.workshop,ad.machineid " +
//				"from (select m.id as mid,m.machinename as machinename,manufacturer,machinetype,standard,operater,workshop,m.machineid as machineid,(select count(mt.machineid) " +
//				"from machinetask as mt where mt.machineid = m.machineid and mt.status='未开工')as tasknum  " +
//				"from pres_machineinfo as m where m.flag = 'y' and m.applyflag='y' and m.machineid " +
//				"in (select mtt.machineid from machinetask as mtt where mtt.status='未开工')) as ad";
		String sql ="select p.*,m.tasknum,t.ordernum from pres_machineinfo p "
			+" left join (select machineid,count(machineid) as tasknum from machinetask where status='未开工' group by machineid) m on p.machineid = m.machineid "
			+" left join (select machineid,count(orderid) as ordernum from taskinfo t left join machinetask m on m.taskid = t.taskid where m.status='未开工' group by machineid) t on p.machineid = t.machineid " 
			+" where m.tasknum is not null ";
		
		doAllSearchList(request, "list", sql, "jdbc");
		
		return SUCCESS;
	}
	/**
	 * @author 韩飞
	 * @time Jan 5, 2012	
	 * @return
	 * @throws Exception 
	 * 方法功能: 对应机台的排产列表
	 */
	public String orderList() throws Exception{
		
//		String sql ="select mt.id as mtid,mt.mtoid,mt.taskid,mt.machineid,mt.orders,mt.status,mt.createtime,mt.finishtime,mt.createuser," +
//				"m.id as mid,m.machinename,m.machinetype,m.status as mstatus,m.orderid,m.workshop," +
//				"t.id as tid,t.taskName,t.printSize,t.amount,t.addAmount" +
//				" from machinetask mt " +
//				" left join pres_machineinfo as m on mt.machineid=m.machineid" +
//				" left join taskinfo as t on mt.taskid=t.taskid where 1=1 and mt.machineid=?" +
//				" order by mt.orders";
		String sql = "select m.*,o.orderid,o.name,o.ordertime,t.taskName,t.printSize,t.amount,t.addAmount,t.subPaper,t.goodsid,g.goodsname,g.standard from machinetask m " 
				+" left join taskinfo t on t.taskid = m.taskid "
				+" left join orderinfo o on t.orderid = o.orderid "
				+" left join pres_makings g on t.goodsid = g.goodsid "
				+" where m.machineid = ? and m.status='未开工' order by IFNULL(CONVERT(m.orders,SIGNED),999),t.orderid,t.taskname,t.createtime";
		//查询对应机台的生产单信息
		request.setAttribute("listinfo", cmd.queryListByPS(sql, new SqlParameter(machineid), null));
		
//		查询人员信息
		request.setAttribute("personlist", new FinancialDao().SelPerson());
//		查询机台信息
		request.setAttribute("machineinfo", cmd.queryObjectByPS("select * from pres_machineinfo where machineid = ?", new SqlParameter(machineid),null));
		
		return SUCCESS;
	}
	
	/**
	 * @author 韩飞
	 * @time Jan 7, 2012	
	 * @return
	 * @throws Exception 
	 * 方法功能:
	 */
	public String saveOrders() throws Exception{
		
		String[] taskid = request.getParameterValues("taskid");
		String[] orders = request.getParameterValues("orders");
		String[] createuser = request.getParameterValues("createuser");
		
		List<String> sqls = new ArrayList<String>();
		for(int i =0;i<taskid.length;i++){
			sqls.add("update machinetask set orders='"+orders[i]+"',createuser='"+createuser[i]+"' where taskid='"+taskid[i]+"'");
		}
		cmd.insorupdArray(sqls);
		
		return SUCCESS;
	}
	
	private String machineid;
	public String getMachineid() {
		return machineid;
	}
	public void setMachineid(String machineid) {
		this.machineid = machineid;
	}
	
}
